home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / pdcurs21.zip / PORTABLE.ZIP / WGETCH.C < prev    next >
Text File  |  1992-11-21  |  6KB  |  172 lines

  1. #define        CURSES_LIBRARY  1
  2. #include <curses.h>
  3. #undef wgetch
  4.  
  5. #ifndef        NDEBUG
  6. char *rcsid_wgetch = "$Header: c:/curses/portable/RCS/wgetch.c%v 2.0 1992/11/15 03:29:25 MH Rel $";
  7. #endif
  8.  
  9. static WINDOW *w;                      /* to reduce stack usage   */
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17. /*man-start*********************************************************************
  18.  
  19.   wgetch()     - read character
  20.  
  21.   X/Open Description:
  22.        A character is read from the terminal associated with the
  23.        window.  In nodelay mode, if there is no input waiting,
  24.        the value ERR is returned.  In delay mode, the program will
  25.        hang until the system passes text through to the program.
  26.        Depending on the setting of cbreak(), this will be after one
  27.        character or after the first newline.  Unless noecho() has
  28.        been set, the character will also be echoed into the designated
  29.        window.
  30.  
  31.        If keypad() is TRUE, and a function key is pressed, the token for
  32.        that function key will be returned instead of the raw characters.
  33.        Possible function keys are defined in <curses.h> with integers
  34.        beginning with 0401, whose names begin with KEY_.  If a character
  35.        is received that could be the beginning of a function key (such as
  36.        escape), curses will set a timer.  If the remainder of the sequence
  37.        does not come in within the designated time, the character will be
  38.        passed through, otherwise the function key value will be returned.
  39.        For this reason, on many terminals, there will be a delay after a
  40.        user presses the escape key before the escape is returned to the
  41.        program.  (Use by a programmer of the escape key for a single
  42.        character function is discouraged.)
  43.  
  44.        NOTE: getch(), mvgetch() and mvwgetch() are macros.
  45.  
  46.   PDCurses Description:
  47.        Given the nature of the PC, there is no such timer set for an
  48.        incoming ESCAPE value, because function keys generate unique
  49.        scan codes that are not prefixed with the ESCAPE character.
  50.  
  51.        Also, note that the getch() definition will conflict  with
  52.        many DOS compiler's runtime libraries.
  53.  
  54.   X/Open Return Value:
  55.        These functions return OK on success and ERR on error.
  56.  
  57.   PDCurses Errors:
  58.        It is an error to call this function with a NULL window pointer.
  59.  
  60.   Portability:
  61.        PDCurses        int wgetch( WINDOW* win );
  62.        X/Open Dec '88  int wgetch( WINDOW* win );
  63.        BSD Curses      int wgetch( WINDOW* win );
  64.        SYS V Curses    int wgetch( WINDOW* win );
  65.  
  66. **man-end**********************************************************************/
  67.  
  68. int    wgetch(WINDOW *win)
  69. {
  70. extern short   c_pindex;               /* putter index            */
  71. extern short   c_gindex;               /* getter index            */
  72. extern short   c_ungind;               /* wungetch() push index   */
  73. extern chtype  c_ungch[NUNGETCH];      /* array of ungotten chars */
  74. extern  WINDOW*        _getch_win_;
  75.  
  76.        signed  key;
  77.        bool    cbr;
  78. static chtype  buffer[_INBUFSIZ];      /* character buffer */
  79.  
  80.        if (win == (WINDOW *)NULL)
  81.                return( ERR );
  82.  
  83.        _getch_win_ = win;
  84.        if (c_ungind)                           /* if ungotten char exists */
  85.                return( c_ungch[--c_ungind] );  /* remove and return it */
  86.        if ((!_cursvar.raw_inp) &&
  87.            (!_cursvar.cbreak))
  88.        {
  89.                /*
  90.                 * if normal
  91.                 */
  92.                if (c_gindex < c_pindex)
  93.                {
  94.                        /*
  95.                         * and data in buffer
  96.                         */
  97.                        return( buffer[c_gindex++] );
  98.                }
  99.        }
  100.  
  101.        w = win;                /* static for speed & stack */
  102.        c_pindex = 0;           /* prepare to buffer data */
  103.        c_gindex = 0;
  104.        for(;;)                 /* loop for any buffering */
  105.        {
  106.                if (_cursvar.raw_inp)
  107.                {
  108.                        /*
  109.                         * get a raw character
  110.                         */
  111.                        key = PDC_rawgetch();
  112.                }
  113.                else
  114.                {
  115.                        /*
  116.                         * get a system character
  117.                         * if break return proper
  118.                         */
  119.                        cbr = PDC_get_ctrl_break();
  120.                        PDC_set_ctrl_break(_cursvar.orgcbr);
  121.                        key = PDC_sysgetch();
  122.                        PDC_set_ctrl_break(cbr);        /* restore as it was */
  123.                }
  124.                if (w->_nodelay && (key == -1))
  125.                {
  126.                        /*
  127.                         * if nodelay and no char
  128.                         */
  129.                        return( ERR );
  130.                }
  131.                if ((key == '\r') &&
  132.                    (_cursvar.autocr) &&
  133.                    (!_cursvar.raw_inp))
  134.                {
  135.                        /*
  136.                         * translate CR
  137.                         */
  138.                        key = '\n';
  139.                }
  140.                if (_cursvar.echo && (key < 0x100))
  141.                {
  142.                        /*
  143.                         * if echo is enabled
  144.                         */
  145.                        waddch(w, key);
  146.                        wrefresh(w);
  147.                }
  148.                if (_cursvar.raw_inp || _cursvar.cbreak)
  149.                {
  150.                        /*
  151.                         * if no buffering
  152.                         */
  153.                        return( key );
  154.                }
  155.  
  156.                if (c_pindex < _INBUFSIZ - 2)
  157.                {
  158.                        /*
  159.                         * if no overflow, put data in buffer
  160.                         */
  161.                        buffer[c_pindex++] = key;
  162.                }
  163.                if ((key == '\n') || (key == '\r'))
  164.                {
  165.                        /*
  166.                         * if we got a line
  167.                         */
  168.                        return( buffer[c_gindex++] );
  169.                }
  170.        }
  171. }
  172.